<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Inter\Controller;

/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class ArticleController extends InterController {
  function __construct() {
	   parent::__construct();
       $this->cat = D("Category");
	  
   }
    /* 文档模型频道页 */
	public function index(){
      echo "Hello 你在干什么";
	}


	/* 获取顶级分类 */
	public function getCate(){
		/* 分类信息 */
		$cate = $this->cat->field('id,title')->where(array('pid'=>0,'status'=>1))->order('sort asc')->select(); 
            $status['code'] = 0;
            //$status['msg'] = '手机号已存在';
            $status['data'] = $cate;
            $this->ajaxReturn($status);
	}
		/* 文档模型详情页 */
	public function detail(){
		$status['code'] = 1;
        $id = intval($_POST['id']);
		$uid = intval($_POST['uid']);
		if(!$id){

			$status['msg'] = '文档ID错误！';
            $this->ajaxReturn($status);
		}
		$Document = D('Document');
		$info = $Document->detail($id);$info['mark']=0;
		$info['url'] = '/index.php?s=/Home/Article/detail/id/'.$info['id'].'.html';
		if($info['id']>0){
			$info['imgurl'] = get_cover($val['cover_id']);
			$info['comment'] = M('comment')->where(array('aid'=>$id))->count();
			$cout = M('collect')->where(array('aid'=>$id,'uid'=>$uid))->count();
			if($cout>0)
			{
				$info['mark']=1;
			}
		} 
        /* 更新浏览数 */
		$map = array('id' => $id);
		$Document->where($map)->setInc('view');
        $status['code'] = 0;
        $status['data'] = $info;
        $this->ajaxReturn($status);
	}	

	/* 根据分类id获取文档列表 */
	public function lists(){
		$status['code'] = 1;
  
        $rows = intval($_GET['rows']) ? intval($_GET['rows']) : 10;
        $cat = intval($_GET['cat']);
		if($cat==0)
		{
			
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$Document = D('Document');
	    $map['category_id'] = $cat;
		$map['status'] = 1;
		//$list = $thiddds->lists($Document,$map,'id DESC');
				$count      = $Document->where($map)->count();
			$Page       = new \Think\Page($count,$rows);  
		$list = $Document->where($map)->order("level desc,update_time desc")->limit($Page->firstRow.','.$Page->listRows)->select(); 
   		if(!empty($list)){

		
			foreach($list as $key=>$val)
			{
				$rs = M('document_article')->where(array('id'=>$val['id']))->find();
		
				$list[$key]['bookmark'] = $rs['bookmark'];
				$list[$key]['content'] = $rs['content'];
				if($rs['video'])
				{
                    $list[$key]['video'] = $rs['video'];
				}
				else if($rs['localvideo']){
                    $list[$key]['video'] = get_cover($rs['localvideo'],'path');
				}
				unset($rs);
				if($val['cover_id']>0){
			      $list[$key]['imgurl'] = get_cover($val['cover_id']);
				}
				
				$list[$key]['comment'] = M('comment')->where(array('aid'=>$val['id']))->count();
			}
		}
		//print_R($list);exit;
              $status['code'] = 0;
			  $status['data'] = $list;
			  $this->ajaxReturn($status);

	}

		/* 根据专题id获取文档列表 */
	public function Speclist(){
		$status['code'] = 1;
        $page = intval($_GET['page']) ? intval($_GET['page']) : 1;
        $rows = intval($_GET['rows']) ? intval($_GET['rows']) : 10;
        $sid = intval($_GET['sid']);
		if($sid==0)
		{
			
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$map['id'] = 0;
		// $rs = M('speclist')->field('aid')->where(array('sid'=>$sid))->select(); //print_R($rs);exit;
		// if($rs[0]['aid']>0)
		// { foreach($rs as $key=>$val)
		// 	{
		// 	 $ids[]= $val['aid'];
			 
		// 	}
  //         $map['id'] = array('in',$ids);
		// }unset($rs);
		// $Document = D('Document');
		// 		$map['status'] = 1;
		// $count      = $Document->where($map)->count();
		// 	$Page       = new \Think\Page($count,$rows);  
		// $list = $Document->where($map)->order('update_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
  //  		if(!empty($list)){

		
		// 	foreach($list as $key=>$val)
		// 	{
		// 		$rs = M('document_article')->field('bookmark')->where(array('id'=>$val['id']))->find();

		// 		$list[$key]['bookmark'] = $rs['bookmark'];
		// 		$list[$key]['comment'] = M('comment')->where(array('aid'=>$val['id']))->count();
		// 		unset($rs);
		
		// 		if($val['cover_id']>0){
		// 	      $list[$key]['imgurl'] = get_cover($val['cover_id']);
		// 		}
		// 	}
		// }
  //             $status['code'] = 0;
		// 	  $status['data'] = $list;
		// 	  $this->ajaxReturn($status);
		$list = M('speclist')->where(array('sid'=>$sid,'d.status'=>1))->join("LEFT JOIN one_document d on d.id=one_speclist.aid")->join("LEFT JOIN one_document_article a on a.id=one_speclist.aid")->order("addtime DESC")->page("$page,$rows")->select();
		if(!empty($list)){
			foreach($list as $key=>$val)
			{
				$list[$key]['imgurl'] = get_cover($val['cover_id']);
				$list[$key]['comment'] = M('comment')->where(array('aid'=>$val['id']))->count();
				unset($list[$key]['content']);
			}
		}
		$status['data'] = $list;
		$this->ajaxReturn($status);
	}

		/* 根据分类id获取专题列表 */
	public function specs(){
		$status['code'] = 1;
        $p = intval($_GET['P']);
        $rows = intval($_GET['rows']) ? intval($_GET['rows']) : 3;
        $cat = intval($_GET['cat']);
		if($cat==0)
		{	
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$spec = M('spec');
		$list = $spec->where(array('cid'=>$cat,'status'=>1))->order('disorder desc,addtime desc')->select();
		if(!empty($list)){
			foreach($list as $key=>$val)
			{
			           $rs = M('speclist')->where(array('sid'=>$val['id']))->order("addtime DESC")->limit(3)->select();
					         if(!empty($rs))
							{
									   foreach($rs as $a=>$v)
								{ 
									   $rs[$a]['comment']  = M('comment')->where(array('aid'=>$v['id']))->count();
									   $view = M('document')->field("view")->where(array('id'=>$v['aid']))->find();
									    $rs[$a]['view']  = $view['view'];
								}
							}
					   
						$list[$key]['doc'] = $rs;
				if($val['thumb']>0)
				{
			      $list[$key]['imgurl'] = get_cover($val['thumb']);
				}
			}
		}
        $status['code'] = 0;
	    $status['data'] = $list;
	    $this->ajaxReturn($status);		
	}

	/* 获取广告图 */
	public function Adlist(){
		$status['code'] = 1;


		/* 获取详细信息 */
		$Document = D('Document');
		$info = $Document->where(array('position'=>16,'status'=>1))->select();
		if(!empty($info)){

		
			foreach($info as $key=>$val)
			{
				if($val['cover_id']>0){
			 $info[$key]['imgurl'] = get_cover($val['cover_id']);}
			}
		}
        $status['code'] = 0;
		$status['data'] = $info;
        $this->ajaxReturn($status);
	}
	/* 获取活动 */
	public function Actilist(){
		$status['code'] = 1;


		/* 获取详细信息 */
		$Document = D('Document');
		$info = $Document->where(array('position'=>32,'status'=>1))->select();
		if(!empty($info)){

		
			foreach($info as $key=>$val)
			{
				if($val['cover_id']>0){
			      $info[$key]['imgurl'] = get_cover($val['cover_id']);
				}
				  $rs = M("document_article")->where(array('id'=>$val['id']))->find();
				 if($rs['localvideo']>0)
				{
                    $video = get_cover($rs['localvideo'],'path');
				}
				else {
                   $video = $rs['video'];
				} unset($rs);
				 $info[$key]['video'] = $video;
			}
		}
        $status['code'] = 0;
		$status['data'] = $info;
        $this->ajaxReturn($status);
	}
	/* 获取幻灯 */
	public function Slidelist(){
		$status['code'] = 1;
		$cat = intval($_POST['cat']);
		/* 标识正确性检测 */
		if($cat==0){
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}

		/* 获取详细信息 */
		$Document = D('Document');
		$info = $Document->where(array('category_id'=>$cat,'status'=>1,'position'=>8))->select();

		if(!empty($info)){
			foreach($info as $key=>$val)
			{
			 if($val['cover_id']>0){
			   $info[$key]['imgurl'] = get_cover($val['cover_id']);
			 }
			}
		}
		
        $status['code'] = 0;
		$status['data'] = $info;
        $this->ajaxReturn($status);
	}

		/* 文章收藏 取消收藏*/
	public function Bmark(){
		$status['code'] = 1;
		$id = intval($_POST['id']);
		$uid = intval($_POST['uid']);
		$met = $_POST['met'];
		/* 标识正确性检测 */
		if($id==0){
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
				if($uid==0){
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		if(!$met){
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$Document = D('Document');
		$article = M('document_article');
        $collect = M('collect');
		if($met=='yes')
		{
		   $data['uid'] = $uid;
		   $data['aid'] = $id;
		   $data['addtime'] = time();
           $collect->add($data);
		   $article->where(array('id'=>$aid))->setInc('bookmark');
		}
		if($met=='no')
		{
		   $map['uid'] = $uid;
		   $map['aid'] = $id;
           $collect->where($map)->delete();
		   $article->where(array('id'=>$aid))->setDec('bookmark');
		}		
        $status['code'] = 0;
		$status['msg'] = '操作成功';
        $this->ajaxReturn($status);
	}

		/* 文章收藏 列表*/
	public function Bmarklist(){
		$status['code'] = 1;
  
        $rows = intval($_GET['rows']) ? intval($_GET['rows']) : 10;
        $uid = intval($_GET['uid']);
		if($uid==0)
		{
			
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$Document = D('Document');
	    $map['uid'] = $uid;
        $collect = M('collect');
		//$list = $thiddds->lists($Document,$map,'id DESC');  ->order("level desc,update_time desc")
				$count      = $collect->where($map)->count();
		if($count<1)
		{
			$status['code'] = 0;
		  	$status['data'] = array();
			  $this->ajaxReturn($status);
		}
			$Page       = new \Think\Page($count,$rows);  
		$arr = $collect->field('aid')->where($map)->limit($Page->firstRow.','.$Page->listRows)->order("addtime desc")->select(); 
		$list = array();
	   	if(!empty($arr)){		
			foreach($arr as $ke=>$va)
			{
			  	$ids[] = $va['aid'];
			}
		}

	  $par['id'] = array("in",$ids);
        $list = $Document->where($par)->select(); //print_R($list);exit;
   		if(!empty($list)){

		
			foreach($list as $key=>$val)
			{
				$rs = M('document_article')->where(array('id'=>$val['id']))->find();
		
				$list[$key]['bookmark'] = $rs['bookmark'];
				$list[$key]['content'] = $rs['content'];
				if($rs['video'])
				{
                    $list[$key]['video'] = $rs['video'];
				}
				else if($rs['localvideo']){
                    $list[$key]['video'] = get_cover($rs['localvideo'],'path');
				}
				unset($rs);
				if($val['cover_id']>0){
			      $list[$key]['imgurl'] = get_cover($val['cover_id']);
				}
				
				$list[$key]['comment'] = M('comment')->where(array('aid'=>$val['id']))->count();
			}
		}
		//print_R($list);exit;
              $status['code'] = 0;
			  $status['data'] = $list;
			  $this->ajaxReturn($status);
	}
			/* 删除 列表*/
	public function delBmark(){
		$status['code'] = 1;

        $id = intval($_POST['id']);
		if($id==0)
		{
			
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
	    $map['id'] = $id;
        $collect = M('collect');
		$collect->where($map)->delete(); 


              $status['code'] = 0;
			  $status['msg'] = '操作成功';
			  $this->ajaxReturn($status);
	}	
	
	/* 搜索*/
	public function serach(){
		$status['code'] = 1;
		$name = $_GET['name'];
        $rows = $_GET['rows'] ? $_GET['rows'] : 10;
		if(!$name){
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$Document = D('Document');
        $map['title'] = array('like','%title%');
				$map['status'] = 1;
		$count      = $Document->where($map)->count();
		$Page       = new \Think\Page($count,$rows);  
		$list = $Document->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();
        $status['code'] = 0;
		$status['data'] = $list;
        $this->ajaxReturn($status);
	}

}
